/*	TargetedAbundanceAnalysisWriter.java
 *
 *	Copyright (c) Manateem Challenge Group, Zaia Group, and contributors 2010
 *
 *	This file is part of Manatee ('this project').
 *
 *	This project is free software: you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the Free
 *	Software Foundation, either version 3 of the License, or (at your option)
 *	any later version.
 *
 *	This project is distributed in the hope that it will be useful, but WITHOUT
 *	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *	FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 *	more details.
 *
 *	You should have received a copy of the GNU General Public License along with
 *	this project. If not, see <http://www.gnu.org/licenses/>.
 */
package edu.bu.manateem.msg.analysis.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Set;

import edu.bu.manateem.msg.MSGConstants;
import edu.bu.manateem.msg.analysis.WidthPair;
import edu.bu.manateem.msg.model.CompoundParent;
import edu.bu.manateem.msg.model.IntensityValue;

import org.apache.commons.math.stat.descriptive.moment.Mean;
import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *	<p>
 *	
 *	</p>
 *
 *
 *	@author
 *		Christopher Jacobs
 *
 *	@version
 *		1.0
 *
 *	@changelog
 *		Jun 10, 2010	- Version 1.0.
 */
public class TargetedAbundanceAnalysisWriter
	extends AnalysisWriter
{
	/*
	 * TYPES
	 */

	/*
	 * CONSTANTS
	 */

	public static final String RAW_OUT = "raw.tsv";
	public static final String PERCENT_OUT = "percent.tsv";
	public static final String PARAM_OUT = "parameters.txt";
	
	/*
	 * FIELDS
	 */

	/*
	 * CONSTRUCTORS
	 */

	public TargetedAbundanceAnalysisWriter()
		throws SecurityException,FileNotFoundException
	{
		super();
	}
	
	/*
	 * INITIALIZERS
	 */

	/*
	 * METHODS
	 */

	public static void generateParameterFile(String file,Set<String> rawFiles,
		String targetFile,WidthPair mzSumWidth,WidthPair rtSumWidth,
		WidthPair rtSearchWidth)
		throws FileNotFoundException
	{
		StringBuilder out = new StringBuilder();
		
		out.append("Raw Input Files:" + MSGConstants.NL);
		for (String rawFile : rawFiles)
		{
			out.append(rawFile + MSGConstants.NL);
		}
		
		out.append(MSGConstants.NL + "Targets File:" + MSGConstants.NL);
		if (targetFile != null)
		{
			out.append(targetFile + MSGConstants.NL);
		}
		
		out.append(MSGConstants.NL + "Options:" + MSGConstants.NL);
		out.append("m/z Sum Width (Left): " + mzSumWidth.left + MSGConstants.NL);
		out.append("m/z Sum Width (Right): " + mzSumWidth.right
			+ MSGConstants.NL);
		out.append("R.T. Sum Width (Left): " + rtSumWidth.left
			+ MSGConstants.NL);
		out.append("R.T. Sum Width (Right): " + rtSumWidth.right
			+ MSGConstants.NL);
		out.append("R.T. Search Width (Left): " + rtSearchWidth.left
			+ MSGConstants.NL);
		out.append("R.T. Search Width (Right): " + rtSearchWidth.right
			+ MSGConstants.NL);
		
		PrintWriter paramWriter = new PrintWriter(new File(file));
		paramWriter.print(out.toString());
		paramWriter.flush();
	}
	
	public void generateAbundanceFilePercent(Set<CompoundParent> compounds)
		throws FileNotFoundException
	{
		StringBuilder out = new StringBuilder();
		
		out.append("Parent Name" + MSGConstants.TAB + "Mean" + MSGConstants.TAB
			+ "Std. Dev.");
		out.append(MSGConstants.NEW_LINE);
		
		Mean mean = new Mean();
		StandardDeviation std = new StandardDeviation();
		
		for (CompoundParent parent : compounds)
		{
			mean.clear();
			std.clear();
			
			Map<String,IntensityValue> abundanceByFile =
				parent.getTotalAbundanceByFile();
			for (String fileKey : abundanceByFile.keySet())
			{
				double intensityPercent =
					abundanceByFile.get(fileKey).getRelativeIntensity() * 100;
				mean.increment(intensityPercent);
				std.increment(intensityPercent);
			}
			out.append(parent.getName());
			out.append(MSGConstants.TAB);
			out.append(mean.getResult());
			out.append(MSGConstants.TAB);
			out.append(std.getResult());
			out.append(MSGConstants.NEW_LINE);
		}
		PrintWriter rawWriter =
			new PrintWriter(new File(directory + MSGConstants.FILE_SEPARATOR
				+ TargetedAbundanceAnalysisWriter.PERCENT_OUT));
		
		rawWriter.print(out.toString());
		rawWriter.flush();
	}
	
	public void generateAbundanceFileRaw(Set<CompoundParent> compounds)
		throws FileNotFoundException
	{
		StringBuilder out = new StringBuilder();
		
		for (CompoundParent parent : compounds)
		{
			out.append(parent.getName());
			Map<String,IntensityValue> abundanceByFile =
				parent.getTotalAbundanceByFile();
			for (String fileKey : abundanceByFile.keySet())
			{
				out.append(MSGConstants.TAB);
				out.append(fileKey);
				out.append(MSGConstants.TAB);
				out.append(abundanceByFile.get(fileKey).getRawIntensity());
				out.append(MSGConstants.TAB);
				out.append(abundanceByFile.get(fileKey).getNumberOfScans());
			}
			out.append(MSGConstants.NEW_LINE);
		}
		PrintWriter rawWriter =
			new PrintWriter(new File(directory + MSGConstants.FILE_SEPARATOR
				+ TargetedAbundanceAnalysisWriter.RAW_OUT));
		
		rawWriter.print(out.toString());
		rawWriter.flush();
		
	}
	
	public void generateAbundanceFiles(Set<CompoundParent> compoundParents)
		throws FileNotFoundException
	{
		generateAbundanceFileRaw(compoundParents);
		generateAbundanceFilePercent(compoundParents);
	}
	
	public void generateParameterFile(Set<String> rawFiles,String targetFile,
		WidthPair mzSumWidth,WidthPair rtSumWidth,WidthPair rtSearchWidth)
		throws FileNotFoundException
	{
		StringBuilder out = new StringBuilder();
		
		out.append("Raw Input Files:" + MSGConstants.NL);
		for (String rawFile : rawFiles)
		{
			out.append(rawFile + MSGConstants.NL);
		}
		
		out.append(MSGConstants.NL + "Targets File:" + MSGConstants.NL);
		out.append(targetFile + MSGConstants.NL);
		
		out.append(MSGConstants.NL + "Options:" + MSGConstants.NL);
		out.append("m/z Sum Width (Left): " + mzSumWidth.left + MSGConstants.NL);
		out.append("m/z Sum Width (Right): " + mzSumWidth.right
			+ MSGConstants.NL);
		out.append("R.T. Sum Width (Left): " + rtSumWidth.left
			+ MSGConstants.NL);
		out.append("R.T. Sum Width (Right): " + rtSumWidth.right
			+ MSGConstants.NL);
		out.append("R.T. Search Width (Left): " + rtSearchWidth.left
			+ MSGConstants.NL);
		out.append("R.T. Search Width (Right): " + rtSearchWidth.right
			+ MSGConstants.NL);
		
		PrintWriter paramWriter =
			new PrintWriter(new File(directory + MSGConstants.FILE_SEPARATOR
				+ TargetedAbundanceAnalysisWriter.PARAM_OUT));
		paramWriter.print(out.toString());
		paramWriter.flush();
	}
	
	public void generatePlotFiles(
		Map<CompoundParent,XYSeriesCollection> plotData)
	{
		final String extenstion = ".jpg";
		
		for (CompoundParent compound : plotData.keySet())
		{
			ChartPlotThread plotThread =
				new ChartPlotThread(compound.getName(),plotData.get(compound),
					new File(plotsDir + MSGConstants.FILE_SEPARATOR
						+ compound.getName() + extenstion));
			plotThread.run();
			
		}
	}
}
